<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use App\Http\Constant;
use App\Helpers\ValidatorResponse;
use App\Helpers\SuccessResponse;
use Illuminate\Support\Facades\DB;
use App\Http\Model\RbacNode;

class AccessController extends Controller {
    //权限列表验证
    public function accessList(Request $request) {
        $is_access = intval($request->input('is_access')) == 1 ? 1 : 0;
        
        //权限数组
        $array  =  [
            '_access-users-parent' =>  [
                 '/api/v1/data/listUser', 
                 '/api/v1/data/listCompany'
            ],
            '_access-users-list'                  =>  '/api/v1/data/listUser',
            '_access-user-add'                    =>  '/api/v1/data/addUser',
            '_access-user-open'                   =>  '/data-api/v1/user/updateStatus',
            '_access-user-close'                  =>  '/data-api/v1/user/updateStatus',
            '_access-user-eidt'                   =>  '/api/v1/data/userDetail',
            '_access-company-list'                =>  '/api/v1/data/listCompany',
            '_access-company-add'                 =>  '/api/v1/data/addCompany',
            '_access-company-edit-access'         =>  '/data-api/v1/company/detail',
            '_access-authtool-parent'             =>  '/other/v1/authtool/index',
            '_access-content-parent'              =>  ['/api/v1/news/listNews', '/api/v1/category/listCategory'],
            '_access-article-category-list'       =>  '/api/v1/category/listCategory',
            '_access-news-category-delete'        =>  '/api/v1/category/delete',
            '_access-news-category-open'          =>  '/api/v1/category/open',
            '_access-news-category-close'         =>  '/api/v1/category/close',
            '_access-news-category-add'           =>  '/api/v1/category/add',
            '_access-news-category-eidt'          =>  '/api/v1/category/detail',
            '_access-article-list'                =>  '/api/v1/news/listNews',
            '_access-news-delete'                 =>  '/api/v1/news/delete',
            '_access-news-open'                   =>  '/api/v1/news/publish',
            '_access-news-close'                  =>  '/api/v1/news/draft',
            '_access-news-add'                    =>  '/api/v1/news/add',
            '_access-news-eidt'                   =>  '/api/v1/news/detail',
            '_access-access-parent'               =>  ['/api/v1/rbacNode/list', '/api/v1/rbacUser/list', '/api/v1/rbacRole/list'],
            '_access-access-node-list'            =>  '/api/v1/rbacNode/list',
            '_access-create-top-node'             =>  '/api/v1/rbacNode/add',
            '_access-edit-node'                   =>  '/api/v1/rbacNode/edit',
            '_access-delete-node'                 =>  '/api/v1/rbacNode/delete',
            '_access-access-user-list'            =>  '/api/v1/rbacUser/list',
            '_access-rbac-user-add'               =>  '/api/v1/rbacUser/add',
            '_access-rbac-user-init-password'     =>  '/api/v1/rbacUser/initPassword',
            '_access-rbac-user-detail'            =>  '/api/v1/rbacUser/detail',
            '_access-rbac-user-delete'            =>  '/api/v1/rbacUser/delete',
            '_access-rbac-user-open'              =>  '/api/v1/rbacUser/open',
            '_access-rbac-user-close'             =>  '/api/v1/rbacUser/close',
            '_access-access-role-list'            =>  '/api/v1/rbacRole/list',
            '_access-rbac-role-add'               =>  '/api/v1/rbacRole/add',
            '_access-rbac-role-delete'            =>  '/api/v1/rbacRole/delete',
            '_access-rbac-role-open'              =>  '/api/v1/rbacRole/open',
            '_access-rbac-role-close'             =>  '/api/v1/rbacRole/close',
            '_access-rbac-role-set-access'        =>  '/api/v1/rbacRole/setAccess',
            '_access-faq-parent'                  =>  ['/oth/er/feedback/list', '/api/v1/faqCate/listCategory', '/api/v1/faq/listFaq'],
            '_access-faq-feedback-list'           =>  '/oth/er/feedback/list',
            '_access-faq-category-list'           =>  '/api/v1/faqCate/listCategory',
            '_access-faq-category-add'            =>  '/api/v1/faqCate/add',
            '_access-faq-category-delete'         =>  '/api/v1/faqCate/delete',
            '_access-faq-category-open'           =>  '/api/v1/faqCate/open',
            '_access-faq-category-close'          =>  '/api/v1/faqCate/close',
            '_access-faq-category-eidt'           =>  '/api/v1/faqCate/detail',
            '_access-faq-list'                    =>  '/api/v1/faq/listFaq',
            '_access-faq-add'                     =>  '/api/v1/faq/add',
            '_access-faq-delete'                  =>  '/api/v1/faq/delete',
            '_access-faq-open'                    =>  '/api/v1/faq/open',
            '_access-faq-close'                   =>  '/api/v1/faq/close',
            '_access-faq-eidt'                    =>  '/api/v1/faq/detail',
            '_access-settings-parent'             =>  ['/oth/er/settings/settings', '/oth/er/settings/site'],
            '_access-settings-settings'           =>  '/oth/er/settings/settings',
            '_access-settings-site'               =>  '/oth/er/settings/site'
        ];
        $rbac_node_model = new RbacNode();
        $arr = array();
        foreach($array as $k=>$v){
            if(is_array($v)){
                $arr[$k] = implode(',',$v);
            } else {
                $arr[$k] = $v;
            }
        }
        $vas    = explode(',',implode(',',array_values($arr)));
        $values = array_unique($vas);
        $info   = $rbac_node_model->getNodeListByNodePath($values);
        
        foreach($array as $k=>$v){
            if(is_array($v)){
                foreach($v as $k2=>$v2){
                    if(in_array($v2,$info)){
                        $arr11[$k][] = $v2;
                    }
                }
            } else {
                if(in_array($v,$info)){
                    $arr11[$k] = $v;
                }
            }
        }
        return SuccessResponse::handler($arr11);
    }
}
